// components/index_message/index_message.js
import tool from '../../modules/tool'

Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    size: 20,
    show: false,
    canGet: true,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleLongTap(e) {
      e = e.currentTarget.dataset
      console.log(e)
      tool.handleShowActionSheet([
        {
          name: '删除',
          callBack: async () => {
            let re = await tool.requestData('/notification/deleteItem', {
              id: e.item.id
            })
            if (re.status) {
              tool.store.setItem('updateNotifications')
            } else {
              tool.handleShowToast(re.content, 'error')
            }
          }
        }
      ])
    },
    async handleToTop() {
      return
      this.setData({
        canGet: true,
        list: []
      })
      await this.get(true)
    },
    handleClick(e) {

      e = e.currentTarget.dataset
      tool.handleNotificationItemClick(e.item)
      tool.postFrontStatus(`点击通知 ${e.item.id}`)
      this.handleSetRead({
        currentTarget: {
          dataset: {
            id: e.item.id
          }
        }
      })
    },
    async get(first = false) {
      if (this.data.canGet === false) return
      let re = await tool.requestData('/notification/getList', {
        finalId: first === true ? undefined : (() => {
          if (this.data.list.length) {
            return this.data.list[this.data.list.length - 1].id
          } else {
            return undefined
          }
        })(),
        size: this.data.size
      }, 'get')
      if (re.status) {
        if (re.data.length == 0) this.setData({
          canGet: false
        })
        this.setData({
          list: [...this.data.list, ...re.data]
        })
      } else {
        throw re.content
      }
    },
    async handleUpdate() {
      let re = await tool.requestData('/notification/getListById', {
        ids: (() => {
          let t = []
          for (let i = 0; i < this.data.list.length; i++) {
            t.push(this.data.list[i].id)
          }
          return t
        })()
      }, 'post', false)
      if (re.status) {
        let t = this.data.list
        for (let i = 0; i < re.data.length; i++) {
          for (let i1 = 0; i1 < t.length; i1++) {
            if (re.data[i].id == t[i1].id) {
              t[i1] = Object.assign(t[i1], re.data[i])
              break
            }
          }
        }
        this.setData({
          list: t
        })
      } else {

      }
    },
    async handleSetRead(e) {
      e = e.currentTarget.dataset
      tool.postFrontStatus(`点击通知已读 ${e.id}`)
      let re = await tool.requestData('/notification/setRead', { id: e.id })
      if (re.status) {
        tool.store.setItem('updateNotifications')
      } else {
        tool.handleShowToast(re.content, 'error')
      }
    }
  },

  async attached() {
    tool.store.onChange('updateNotifications', () => this.handleUpdate())
    tool.flow(async () => {
      await this.get(true)
      this.setData({
        show: true
      })
    }, err => tool.handleShowToast(`${err}`, 'error'))
  }
})